package com.java.controller;

import com.java.entity.Paper;
import com.java.service.PaperService;
import com.java.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class PaperController extends BaseController {

    @Autowired
    private PaperService paperService;

    @PostMapping("paper/insert")
    public ResultVo insert(@RequestBody Paper paper) {
        int rows = paperService.insert(paper);
        return toVo(rows);
    }

    @PostMapping("paper/delete")
    public ResultVo delete(@RequestBody String paperId) {
        if (!StringUtils.hasText(paperId)){
            throw new RuntimeException("paperId is null");
        }
        int rows = paperService.delete(Integer.valueOf(paperId));
        return toVo(rows);
    }

    @PostMapping("paper/update")
    public ResultVo update(@RequestBody Paper paper) {
        int rows = paperService.update(paper);
        return toVo(rows);
    }

    @PostMapping("paper/selectOne")
    public ResultVo selectOne(@RequestBody String paperId) {
        if (!StringUtils.hasText(paperId)){
            throw new RuntimeException("paperId is null");
        }
        Paper paper = paperService.selectOne(Integer.valueOf(paperId));
        return ResultVo.success(paper);
    }

    @PostMapping("paper/selectAll")
    public ResultVo selectAll() {
        List<Paper> list = paperService.selectAll();
        return ResultVo.success(list);
    }

    @PostMapping("paper/selectByStatus")
    public ResultVo selectByStatus(@RequestBody String status) {
        if (!StringUtils.hasText(status)){
            throw new RuntimeException("status is null");
        }
        List<Paper> list = paperService.selectByStatus(Integer.valueOf(status));
        return ResultVo.success(list);
    }

    @PostMapping("paper/distributeQuestion")
    public ResultVo distributeQuestion(@RequestBody Map<String, Object> map) {
        Integer paperId = (Integer) map.remove("paperId");
        List<Integer> questionIds = (List<Integer>) map.remove("questionIds");
        int rows = paperService.distributeQuestion(paperId, questionIds);
        return toVo(rows);
    }

    @PostMapping("paper/selectPaperQuestionByPaperId")
    public ResultVo selectPaperQuestionByPaperId(@RequestBody String paperId) {
        if (!StringUtils.hasText(paperId)){
            throw new RuntimeException("paperId is null");
        }
        Paper paper = paperService.selectPaperQuestionByPaperId(Integer.valueOf(paperId));
        return ResultVo.success(paper);
    }

    @PostMapping("paper/selectPaperQuestionOptionByPaperId")
    public ResultVo selectPaperQuestionOptionByPaperId(@RequestBody String paperId) {
        if (!StringUtils.hasText(paperId)){
            throw new RuntimeException("paperId is null");
        }
        Paper paper = paperService.selectPaperQuestionOptionByPaperId(Integer.valueOf(paperId));
        return ResultVo.success(paper);
    }

    @PostMapping("paper/deletePaperQuestionByPaperId")
    public ResultVo deletePaperQuestionByPaperId(@RequestBody String paperId) {
        if (!StringUtils.hasText(paperId)){
            throw new RuntimeException("paperId is null");
        }
        int rows = paperService.deletePaperQuestionByPaperId(Integer.valueOf(paperId));
        return toVo(rows);
    }

}
